﻿/******************************************************************************
 * 【本类功能概述】                                 					      *
 *  版权所有（C）2017- 匠梦科技                                         *
 *  保留所有权利。                                                            *
 ******************************************************************************
 *  作者 : <hughjk>
 *  创建时间:2021年1月6日 14:06:11
 *  文件描述:工具窗口
 *****************************************************************************/
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using Util;
using Object = UnityEngine.Object;

namespace UtilEditors
{

    public partial class SubsidiaryWindow :BaseEditorWindow
    {
        public List<Inspector> Alllist = new List<Inspector>();

        [MenuItem("Tools/辅助/BaseWindow")]
        public static void Init()
        {
            Init<SubsidiaryWindow>();
        }
        public override void OnEnable()
        {
            Inits();
        }

        private void Inits()
        {
            InspectorStart();
            for(int k = 0; k < Alllist.Count; k++)
            {
                Alllist[k].Init(this,k);
            }
            OnSelectionChange();
            Vector2 pos = new Vector2(0,260);
            for(int k = 0; k < Alllist.Count; k++)
            {
                if(Alllist[k].IsShow)
                {
                    Alllist[k].rect.x = pos.x;
                    Alllist[k].rect.y = pos.y;
                    pos.x += Alllist[k].rect.width + 10;
                }
            }

            mainMenu = new MainMenu(this);

        }
        public void AddInspector(Inspector inspector)
        {
            Alllist.Add(inspector);
            inspector.Init(this,Alllist.Count);
            OnSelectionChange();
        }
        void InspectorStart()
        {
            Alllist.Clear();
            Alllist.Add(new TransformEditers());
            Alllist.Add(new FileEditers());
            Alllist.Add(new RigidbodyEditers());
            Alllist.Add(new Rigidbody2DEditers());
            Alllist.Add(new SpriteRendererEditers());
            Alllist.Add(new PolygonCollder2dEditers());

            //继承了 <T> 的父类层级当作不存在了
            //var types = Assembly.GetCallingAssembly().GetTypes();
            //var aType = typeof(Inspector);
            //foreach (var type in types)
            //{
            //    var baseType = type.BaseType;  //获取基类
            //    while (baseType != null)  //获取所有基类
            //    {
            //        if (baseType.Name == aType.Name)
            //        {
            //            debug.log(baseType.Name, aType.Name);
            //            Type objtype = Type.GetType(type.FullName, true);
            //            object obj = Activator.CreateInstance(objtype);
            //            if (obj != null)
            //            {
            //                Inspector info = obj as Inspector;
            //                Alllist.Add(info);
            //            }
            //            break;
            //        }
            //        else
            //        {
            //            baseType = baseType.BaseType;
            //        }
            //    }
            //}
        }
        public override void OnFocus()
        {
            OnSelectionChange();
        }

        public override void OnLostFocus()
        {
            OnSelectionChange();
        }
        //选择其他对象
        public override void OnSelectionChange()
        {
            for(int k = 0; k < Alllist.Count; k++)
            {
                Alllist[k].SelectionChange();
            }
            Repaint();
        }
        private void Update()
        {
            for(int i = 0; i < Alllist.Count; i++)
            {
                Alllist[i].Updata();
            }
        }

        public virtual void OnInspectorUpdate()
        {
            for(int i = 0; i < Alllist.Count; i++)
            {
                Alllist[i].OnInspectorUpdate();
            }
            //Debug.Log("当属性界面更新时");//几乎一直在更新
        }
        Vector2 size;
        public override void OnGUI()
        {

            size = GUILayout.BeginScrollView(size);
            GUILayoutCom.ComX(() =>
            {
                mainMenu.OnGUI();
                GUILayout.FlexibleSpace();//空白
            });
            //GUILayout.FlexibleSpace();//空白 
            GUILayoutCom.ComX(() =>
            {
                for(int i = 0; i < Alllist.Count; i++)
                {
                    Alllist[i].Draw();
                }
                GUILayout.FlexibleSpace();//空白
            });
            GUILayout.EndScrollView();

        }

        public class MainMenu :GUILayoutCom
        {
            public SubsidiaryWindow MainWin;
            float mSlider;
            bool isShowPos = false;
            bool isTimeSlider = false;
            float mtimers = 100;
            bool isDraw = true;
            EditorWindow GameView;

            public MainMenu(SubsidiaryWindow subsidiaryWindow)
            {
                MainWin = subsidiaryWindow;
                Init();
            }
            public virtual void Init()
            {

            }
            Object obj = null;
            public void 显示位置()
            {
                isShowPos = EditorGUILayout.Foldout(isShowPos,"显示位置:" + SelectionObj.Length.toString());
                if(isShowPos)
                {
                    for(int i = 0; i < SelectionObj.Length; i++)
                    {

                        SelectionObj[i] = EditorGUILayout.ObjectField(SelectionObj[i],typeof(GameObject),true,GUILayout.MaxWidth(70)) as GameObject;
                        EditorGUILayout.Vector3Field("",SelectionObj[i].transform.position);
                        if(SelectionObj[i].activeSelf)
                        {
                            if(Button("See"))
                            {
                                var _sceneView = SceneView.lastActiveSceneView;
                                if(_sceneView)
                                {
                                    Vector3 pos = SelectionObj[i].transform.position;
                                    pos.z = -20;
                                    _sceneView.pivot = pos;
                                }
                            }
                        }
                    }
                }
            }
            public override void OnGUI()
            {
                ComY(OnSysSubGUI);

                OnSubGUI();
            }
            public virtual void OnSysSubGUI()
            {
                显示位置();
                if(Button("收Scene"))
                {
                    UtilEditor.收Scene();
                }
                if(Button("收Assets"))
                {
                    UtilEditor.SetProjectBrowserFoldersCollapsed();
                }
                if(Button("收起组件"))
                {
                    UtilEditor.收起组件();
                }
                if(Button("展开组件"))
                {
                    UtilEditor.展开组件();
                }
                if(EditorApplication.isPlaying)
                {
                    if(Camera.main)
                    {
                        mSlider = Camera.main.fieldOfView;
                        mSlider = GUILayout.HorizontalScrollbar(mSlider,1,1,180);
                        Camera.main.fieldOfView = mSlider;
                    }
                    Toggle("",ref isTimeSlider);
                    if(isTimeSlider)
                    {
                        mtimers = GUILayout.HorizontalScrollbar(mtimers,1,1,150);
                        Time.timeScale = mtimers * 0.01f;
                    }
                }
                if(GameView.isNull())
                {
                    var ew = EditorWindow.focusedWindow;
                    //debug.log(ew.ToString());
                    if(ew)
                        if(ew.ToString() == " (UnityEditor.GameView)")
                        {
                            GameView = ew;
                        }
                }
                if(Button("全屏"))
                {
                    if(GameView.isTrue())
                    {
                        GameView.Focus();
                        GameView.maximized = !GameView.maximized;
                    }
                }
                Toggle("绘制",ref isDraw);
                debug.Draw().isdebug = isDraw;
                BookmarkMgr.Ins.OnGUI();
            }
            public virtual void OnSubGUI() { }
        }
    }
}
